﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using Database.Classes;
using System.Collections.Generic;
using System.Text;


public partial class StoredProcedures
{
    [Microsoft.SqlServer.Server.SqlProcedure]
    public static void SentFleet(int Owner, int fromSys, int fromPlanet, int toSys, int toPlanet, int cs, int tr, int ds, int cr, int bs)
    {
        int energy = (int)UserDefinedFunctions.GetPlayerScience(Owner, 3).Value;
        int speed = UserDefinedFunctions.GetPlayerInt(Owner, "Speed").Value;
        List<int> s1 = UserDefinedFunctions.SystemCoordinates(fromSys);
        List<int> s2 = UserDefinedFunctions.SystemCoordinates(toSys);
        int planetOwner = UserDefinedFunctions.PlanetOwner(toSys, toPlanet).Value;
        int same;
        if (Owner == planetOwner) same = 1; else same = 0;
        double tt = Equations.TravelTime(s1[0], s2[0], s1[1], s2[1], fromPlanet, toPlanet, same, energy, speed);
        int h = (int)tt;
        int m = (int)(tt - h * 1.0) ;
        int s = (int)(tt - h * 1.0 - m * 1.0);
        TimeSpan spa = new TimeSpan(h, m, s);
        DateTime arrivialTime = DateTime.Now.Add(spa);
        if (planetOwner > 1 && Owner != planetOwner)
        {
            StringBuilder sb = new StringBuilder("Incoming fleet of player: <a href=\"PlayerInfo.aspx?pId="+Owner.ToString()+"\">" + UserDefinedFunctions.PlayerName(Owner) + "</a><br />");
            if (tr > 0) sb.Append("\tTransports: " + tr.ToString() + "<br />");
            if (ds > 0) sb.Append("\tDestroyers: " + ds.ToString() + "<br />");
            if (cr > 0) sb.Append("\tCruisers: " + cr.ToString() + "<br />");
            if (bs > 0) sb.Append("\tBattleships: " + bs.ToString() + "<br />");
            sb.Append("Will arrive at: " + arrivialTime.ToString()).Append("<br />To planet: ");
            sb.Append(UserDefinedFunctions.SystemName(toSys)).Append(" #").Append(toPlanet.ToString());
            InsertNews(planetOwner, arrivialTime, sb.ToString());
        }

        using (SqlConnection conn = new SqlConnection(StaticMethods.ConnectionString()))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand("Insert Into Fleets Values (@o, @cs, @tr, @ds, @cr, @bs, @fs, @fp, @ts, @tp, @at)", conn))
            {
                cmd.Parameters.Add("@o", SqlDbType.Int).Value = Owner;
                cmd.Parameters.Add("@cs", SqlDbType.Int).Value = cs;
                cmd.Parameters.Add("@tr", SqlDbType.Int).Value = tr;
                cmd.Parameters.Add("@ds", SqlDbType.Int).Value = ds;
                cmd.Parameters.Add("@cr", SqlDbType.Int).Value = cr;
                cmd.Parameters.Add("@bs", SqlDbType.Int).Value = bs;
                cmd.Parameters.Add("@fs", SqlDbType.Int).Value = fromSys;
                cmd.Parameters.Add("@fp", SqlDbType.Int).Value = fromPlanet;
                cmd.Parameters.Add("@ts", SqlDbType.Int).Value = toSys;
                cmd.Parameters.Add("@tp", SqlDbType.Int).Value = toPlanet;

                cmd.Parameters.Add("@at", SqlDbType.DateTime).Value = arrivialTime;
                cmd.ExecuteNonQuery();
                cmd.CommandText = "Update planets SET ColonyShips = ColonyShips-@cs, Transports = Transports-@tr, Destroyers=Destroyers-@ds, Crusiers=Crusiers-@cr, Battleships=Battleships-@bs Where PlanetID = @fp and SystemId = @fs";
                cmd.ExecuteNonQuery();

            }
        }
        CheckPlanet(fromSys, fromPlanet);
    }
    /// <summary>
    /// Preform check if planet is empty and have proper fleetOwnerId in case it was sieged and siege was removed.
    /// </summary>
    /// <param name="fromSys"></param>
    /// <param name="fromPlanet"></param>
    [Microsoft.SqlServer.Server.SqlProcedure]
    private static void CheckPlanet(int sys, int planet)
    {
        int ownerId = UserDefinedFunctions.PlanetOwner(sys, planet).Value ;
        int fleetOwner = UserDefinedFunctions.FleetOnPlanetOwner(sys, planet).Value ;
        if (fleetOwner != ownerId || fleetOwner == 0) return;
        Dictionary<string, int> fleet = UserDefinedFunctions.FleetOnPlanet(sys, planet);
        if (fleet["ColonyShips"] == 0 && fleet["Transports" ]== 0 && fleet["Destroyers"] == 0 && fleet["Crusiers" ]== 0 && fleet["Battleships"] == 0)
        {//sieged planet is now free
            using (SqlConnection conn = new SqlConnection(StaticMethods.ConnectionString()))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand("Update planets SET FleetOwnerID = @fid Where PlanetID = @fp and SystemId = @fs", conn))
                {
                    cmd.Parameters.Add("@fid", SqlDbType.Int).Value = ownerId;
                    cmd.Parameters.Add("@fp", SqlDbType.Int).Value = planet;
                    cmd.Parameters.Add("@fs", SqlDbType.Int).Value = sys;
                    cmd.ExecuteNonQuery();
                }
            }
        }
    }
};
